/* SwingRobot - Decompiled by JCavaj
 * Visit http://www.bysoft.se/sureshot/jcavaj/
 */
package TestTools;
import java.awt.AWTException;
import java.awt.Robot;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Hashtable;
import java.util.StringTokenizer;

public class SwingRobot
{
    private Hashtable mKeys = new Hashtable();
    
    public SwingRobot() {
	mKeys.put("0", new Integer(48));
	mKeys.put("1", new Integer(49));
	mKeys.put("2", new Integer(50));
	mKeys.put("3", new Integer(51));
	mKeys.put("4", new Integer(52));
	mKeys.put("5", new Integer(53));
	mKeys.put("6", new Integer(54));
	mKeys.put("7", new Integer(55));
	mKeys.put("8", new Integer(56));
	mKeys.put("9", new Integer(57));
	mKeys.put("a", new Integer(65));
	mKeys.put("b", new Integer(66));
	mKeys.put("c", new Integer(67));
	mKeys.put("d", new Integer(68));
	mKeys.put("e", new Integer(69));
	mKeys.put("f", new Integer(70));
	mKeys.put("g", new Integer(71));
	mKeys.put("h", new Integer(72));
	mKeys.put("i", new Integer(73));
	mKeys.put("j", new Integer(74));
	mKeys.put("k", new Integer(75));
	mKeys.put("l", new Integer(76));
	mKeys.put("m", new Integer(77));
	mKeys.put("n", new Integer(78));
	mKeys.put("o", new Integer(79));
	mKeys.put("p", new Integer(80));
	mKeys.put("q", new Integer(81));
	mKeys.put("r", new Integer(82));
	mKeys.put("s", new Integer(83));
	mKeys.put("t", new Integer(84));
	mKeys.put("u", new Integer(85));
	mKeys.put("v", new Integer(86));
	mKeys.put("w", new Integer(87));
	mKeys.put("x", new Integer(88));
	mKeys.put("y", new Integer(89));
	mKeys.put("z", new Integer(90));
	mKeys.put("A", new Integer(65));
	mKeys.put("B", new Integer(66));
	mKeys.put("C", new Integer(67));
	mKeys.put("D", new Integer(68));
	mKeys.put("E", new Integer(69));
	mKeys.put("F", new Integer(70));
	mKeys.put("G", new Integer(71));
	mKeys.put("H", new Integer(72));
	mKeys.put("I", new Integer(73));
	mKeys.put("J", new Integer(74));
	mKeys.put("K", new Integer(75));
	mKeys.put("L", new Integer(76));
	mKeys.put("M", new Integer(77));
	mKeys.put("N", new Integer(78));
	mKeys.put("O", new Integer(79));
	mKeys.put("P", new Integer(80));
	mKeys.put("Q", new Integer(81));
	mKeys.put("R", new Integer(82));
	mKeys.put("S", new Integer(83));
	mKeys.put("T", new Integer(84));
	mKeys.put("U", new Integer(85));
	mKeys.put("V", new Integer(86));
	mKeys.put("W", new Integer(87));
	mKeys.put("X", new Integer(88));
	mKeys.put("Y", new Integer(89));
	mKeys.put("Z", new Integer(90));
	mKeys.put(" ", new Integer(32));
	mKeys.put("space", new Integer(32));
	mKeys.put("enter", new Integer(10));
	mKeys.put("shift", new Integer(16));
	mKeys.put("esc", new Integer(27));
	mKeys.put("del", new Integer(127));
	mKeys.put("backspace", new Integer(8));
	mKeys.put("capslock", new Integer(20));
	mKeys.put("ctrl", new Integer(17));
	mKeys.put("alt", new Integer(18));
	mKeys.put("tab", new Integer(9));
	mKeys.put(".", new Integer(46));
	mKeys.put("down", new Integer(40));
	mKeys.put("up", new Integer(38));
	mKeys.put("left", new Integer(37));
	mKeys.put("right", new Integer(39));
	mKeys.put("-", new Integer(45));
    }
    
    public void runScript(String[] strings) {
	Robot robot = null;
	Object object = null;
	if (strings.length < 1) {
	    System.err.println
		("Usage: provide the application name and a robot script file (.rsf)");
	    System.exit(0);
	}
	if (strings.length < 2) {
	    System.err.println
		("Missing command line parameter: robot script file (.rsf)");
	    System.exit(0);
	}
	try {
	    String string = strings[0];
	    ClassLoader classloader = new ClassLoader() {
	    };
	    Class var_class = classloader.loadClass(string);
	    Object object_1_ = var_class.newInstance();
	} catch (Exception exception) {
	    System.out.println("Error: " + exception);
	    exception.printStackTrace();
	}
	Object object_2_ = null;
	BufferedReader bufferedreader = null;
	try {
	    FileReader filereader = new FileReader(strings[1]);
	    bufferedreader = new BufferedReader(filereader);
	} catch (Exception exception) {
	    exception.printStackTrace();
	    System.err.println("Unable to open script file: " + strings[0]);
	    System.exit(0);
	}
	try {
	    robot = new Robot();
	} catch (AWTException awtexception) {
	    System.err.println("Can't start Robot: " + awtexception);
	    System.exit(0);
	}
	robot.delay(2000);
	int i;
	if (strings.length >= 3)
	    i = Integer.parseInt(strings[2]);
	else
	    i = 500;
	robot.setAutoDelay(i);
	try {
	    String string;
	    while ((string = bufferedreader.readLine()) != null) {
		string = string.trim();
		if (string.length() > 0 && !string.startsWith("//")
		    && !string.startsWith("--")) {
		    if (string.startsWith("mouse_move")) {
			string = string.replace(',', ' ');
			StringTokenizer stringtokenizer
			    = new StringTokenizer(string, " ");
			if (stringtokenizer.countTokens() < 3)
			    throw new Exception
				      ("***** invalid script command: "
				       + string);
			stringtokenizer.nextToken();
			robot.mouseMove(Integer.parseInt(stringtokenizer
							     .nextToken()),
					Integer.parseInt(stringtokenizer
							     .nextToken()));
		    } else if (string.startsWith("mouse_press")) {
			string = string.replace(',', ' ');
			StringTokenizer stringtokenizer
			    = new StringTokenizer(string, " ");
			if (stringtokenizer.countTokens() < 3)
			    throw new Exception
				      ("***** invalid script command: "
				       + string);
			stringtokenizer.nextToken();
			robot.mouseMove(Integer.parseInt(stringtokenizer
							     .nextToken()),
					Integer.parseInt(stringtokenizer
							     .nextToken()));
			robot.mousePress(16);
			robot.mouseRelease(16);
		    } else if (string.startsWith("mouse_down"))
			robot.mousePress(16);
		    else if (string.startsWith("mouse_up"))
			robot.mouseRelease(16);
		    else if (string.startsWith("press_key")) {
			StringTokenizer stringtokenizer
			    = new StringTokenizer(string, " ");
			if (stringtokenizer.countTokens() < 2)
			    throw new Exception
				      ("***** invalid script command: "
				       + string);
			stringtokenizer.nextToken();
			Integer integer
			    = (Integer) mKeys.get(stringtokenizer.nextToken
						      ().toLowerCase());
			if (integer == null)
			    throw new Exception("***** unrecognized key name: "
						+ string);
			robot.keyPress(integer.intValue());
		    } else if (string.startsWith("release_key")) {
			StringTokenizer stringtokenizer
			    = new StringTokenizer(string, " ");
			if (stringtokenizer.countTokens() < 2)
			    throw new Exception
				      ("***** invalid script command: "
				       + string);
			stringtokenizer.nextToken();
			Integer integer
			    = (Integer) mKeys.get(stringtokenizer.nextToken
						      ().toLowerCase());
			if (integer == null)
			    throw new Exception("***** unrecognized key name: "
						+ string);
			robot.keyRelease(integer.intValue());
		    } else if (string.startsWith("type_key")) {
			StringTokenizer stringtokenizer
			    = new StringTokenizer(string, " ");
			if (stringtokenizer.countTokens() < 2)
			    throw new Exception
				      ("***** invalid script command: "
				       + string);
			stringtokenizer.nextToken();
			Integer integer
			    = (Integer) mKeys.get(stringtokenizer.nextToken
						      ().toLowerCase());
			if (integer == null)
			    throw new Exception("***** unrecognized key name: "
						+ string);
			robot.keyPress(integer.intValue());
			robot.keyRelease(integer.intValue());
		    } else if (string.startsWith("type_string")) {
			string = string.substring(12).trim();
			robot.setAutoDelay(1);
			for (int i_3_ = 0; i_3_ < string.length(); i_3_++) {
			    char c = string.charAt(i_3_);
			    if (c >= 'A' && c <= 'Z')
				robot.keyPress(16);
			    Integer integer
				= (Integer) mKeys.get(String.valueOf(c));
			    robot.keyPress(integer.intValue());
			    robot.keyRelease(integer.intValue());
			    robot.keyRelease(16);
			}
			robot.setAutoDelay(i);
		    } else if (string.startsWith("wait")) {
			StringTokenizer stringtokenizer
			    = new StringTokenizer(string, " ");
			if (stringtokenizer.countTokens() < 2)
			    throw new Exception
				      ("***** invalid script command: "
				       + string);
			stringtokenizer.nextToken();
			robot.delay(Integer.parseInt(stringtokenizer
							 .nextToken()));
		    } else
			throw new Exception("***** invalid script command: "
					    + string);
		}
	    }
	} catch (Exception exception) {
	    exception.printStackTrace();
	}
	try {
	    if (bufferedreader != null)
		bufferedreader.close();
	} catch (Exception exception) {
	    exception.printStackTrace();
	}
	robot.keyRelease(17);
	robot.keyRelease(18);
    }
    
    public static void main(String[] strings) throws AWTException {
	new SwingRobot().runScript(strings);
	System.exit(0);
    }
}
